﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Restaurant.DAO
{
    public class KhuVucDB
    {
        private SqlConnection con = KetNoiDB.TaoKetNoi();

        public DataTable LoadKhuVuc()
        {
            con.Open();
            string strcmd = "dbo.spLoadKhuVuc";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public bool ThemKhuVuc(int makhu, string ten, int manh)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemKhuVuc";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKhu", makhu));
                cmd.Parameters.Add(new SqlParameter("@tenKhu", ten));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaKhuVuc(int ma)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spXoaKhuVuc";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool SuaKhuVuc(int makhu, string ten, int manh)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spSuaKhuVuc";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maKhu", makhu));
                cmd.Parameters.Add(new SqlParameter("@tenKhu", ten));
                cmd.Parameters.Add(new SqlParameter("@maNH", manh));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public DataTable TimKhuVucID(int ma)
        {
            con.Open();
            string strcmd = "dbo.spTimKhuVucID";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ma", ma));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimKhuVucMaNH(int manh)
        {
            con.Open();
            string strcmd = "dbo.spTimKhuVucNhaHang";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@ma", manh));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable TimKhuVucTen(string ten)
        {
            con.Open();
            string strcmd = "dbo.spTimKhuVucTen";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@tenKhu", ten));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }
    }
}
